package com.caesi.tacacaesifinal;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.caesi.tacacaesifinal.model.Equipe;

public class EquipeListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private List<Equipe> equipes;

    private View selectedView;

    private int lastPosition = -1;

    public EquipeListAdapter(Context contexto, List<Equipe> itens) {
        this.equipes = itens;
        mInflater = LayoutInflater.from(contexto);

    }

    @Override
    public int getCount() {

        return equipes.size();
    }

    @Override
    public Object getItem(int arg0) {

        return equipes.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Equipe item = equipes.get(position);
        convertView = mInflater.inflate(R.layout.item_equipe, null);

        String id = item.getNome();
        ((TextView) convertView.findViewById(R.id.id_equipe_item)).setText(id);
        String resumo = "";
        for(String nome : item.getJogadores()){
            resumo += nome + ", ";
        }
        
        int min = (resumo.length() -2) < 20 ? resumo.length() -2 : 20; 
        resumo = resumo.substring(0, min);
        resumo += "...";
        ((TextView) convertView.findViewById(R.id.resumo_equipe)).setText(resumo);
        ((ImageView) convertView.findViewById(R.id.imagemview))
                .setImageResource(R.drawable.bola);

        return convertView;

    }

    public void setSelectedView(View selectedView, int position) {
        this.lastPosition = position;
        if (this.selectedView != null) {
            this.selectedView.setBackgroundColor(Color.BLACK);
        }
        this.selectedView = selectedView;
        this.selectedView.setBackgroundResource(R.drawable.row_list);
        // this.selectedView.setBackgroundColor(Color.argb(50, 135, 206, 255));
    }

}
